package com.share.two.aio;

import java.net.InetSocketAddress;
import java.nio.channels.AsynchronousChannelGroup;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Server {

	public AsynchronousServerSocketChannel assc;

	public Server(int port){
		try {
			ExecutorService threadPool = Executors.newCachedThreadPool();
			AsynchronousChannelGroup threadGroup = AsynchronousChannelGroup.withCachedThreadPool(threadPool, 1);
			assc = AsynchronousServerSocketChannel.open(threadGroup);
			assc.bind(new InetSocketAddress(12345));
			assc.accept(this, new ServerCompletionHandler());

			System.out.println("server：  accept()是非阻塞的");

			Thread.sleep(Integer.MAX_VALUE);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		Server server = new Server(12345);
	}

}
